package com.start_eternity.oasissafewalk.service.impl;

import com.start_eternity.oasissafewalk.mapper.OrderDetailsMapper;
import com.start_eternity.oasissafewalk.pojo.Logg;
import com.start_eternity.oasissafewalk.pojo.OrderDetails;
import com.start_eternity.oasissafewalk.service.LoggService;
import com.start_eternity.oasissafewalk.service.OrderDetailsService;
import com.start_eternity.oasissafewalk.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * author:ZhangT
 * Date:2024 / 03 / 30 / 10:07
 * Description:
 */
@Service
public class OrderDetailsServiceImpl implements OrderDetailsService {

    @Autowired
    private OrderDetailsMapper orderDetailsMapper;

    @Autowired
    private LoggService loggService;

    @Autowired
    private HttpServletRequest request;

    //查询
    @Override
    public OrderDetails queryById(Integer id) {
        OrderDetails orderDetails = orderDetailsMapper.queryById(id);
        return orderDetails;
    }

    //查询
    @Override
    public OrderDetails queryByNo(String no) {
        OrderDetails orderDetails = orderDetailsMapper.queryByNo(no);
        return orderDetails;
    }

    //添加
    @Override
    public int insertDetails(OrderDetails orderDetails) {
        int i = orderDetailsMapper.insertDetails(orderDetails);

        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行插入order_details,插入的内容为:" + orderDetails);
        loggService.insert(logg);

        return i;
    }

    //修改
    @Override
    public int updateDetailsByNo(OrderDetails orderDetails) {
        int i = orderDetailsMapper.updateDetailsByNo(orderDetails);

        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据 DetailsByNo 修改订单,修改订单内容为:" + orderDetails);
        loggService.insert(logg);

        return i;
    }

    //修改
    @Override
    public int updateDetailsById(OrderDetails orderDetails) {
        int i = orderDetailsMapper.updateDetailsById(orderDetails);

        Logg logg = new Logg();
        logg.setLoggTime(LocalDateTime.now());
        String jwt = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(jwt);
        String name = (String)claims.get("username");
        logg.setLoggName(name);
        logg.setLoggDescription("执行根据 DetailsById 修改订单,修改订单内容为:" + orderDetails);
        loggService.insert(logg);

        return i;
    }
}
